import java.awt.Component;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.image.PixelGrabber;
import java.io.DataInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.swing.ImageIcon;

public final class Sprite extends Renderer2D {
	public int height;
	private int offsetX;
	private int offsetY;
	public int pixels[];
	public int trimHeight;
	public int trimWidth;
	public int width;

	/**
	 * Creates an image from the cache.
	 * 
	 * @param archive
	 *            The archive being read.
	 * @param archiveName
	 *            The name of the archive the image is located in.
	 * @param requestIndex
	 *            The index of the image.
	 */
	public Sprite(Archive archive, String archiveName, int requestIndex) {
		Packet data = new Packet(archive.getFile(archiveName + ".dat"));
		Packet index = new Packet(archive.getFile("index.dat"));
		index.offset = data.getUnsignedShort();
		trimWidth = index.getUnsignedShort();
		trimHeight = index.getUnsignedShort();
		int j = index.getUnsignedByte();
		int srcPixels[] = new int[j];
		for (int loop = 0; loop < j - 1; loop++) {
			srcPixels[loop + 1] = index.get24BitInt();
			if (srcPixels[loop + 1] == 0) {
				srcPixels[loop + 1] = 1;
			}
		}
		for (int l = 0; l < requestIndex; l++) {
			index.offset += 2;
			data.offset += index.getUnsignedShort() * index.getUnsignedShort();
			index.offset++;
		}
		offsetX = index.getUnsignedByte();
		offsetY = index.getUnsignedByte();
		width = index.getUnsignedShort();
		height = index.getUnsignedShort();
		int i1 = index.getUnsignedByte();
		int size = width * height;
		pixels = new int[size];
		if (i1 == 0) {
			for (int k1 = 0; k1 < size; k1++) {
				pixels[k1] = srcPixels[data.getUnsignedByte()];
			}
			return;
		}
		if (i1 == 1) {
			for (int l1 = 0; l1 < width; l1++) {
				for (int i2 = 0; i2 < height; i2++) {
					pixels[l1 + i2 * width] = srcPixels[data.getUnsignedByte()];
				}
			}
		}
	}

	/**
	 * Old method of loading the title screen.
	 * 
	 * @param data
	 *            The Archive data.
	 * @param component
	 *            The component.
	 */
	public Sprite(byte data[], Component component) {
		try {
			// Image image =
			// Toolkit.getDefaultToolkit().getImage(location+"mopar.jpg");
			Image image = Toolkit.getDefaultToolkit().createImage(data);
			MediaTracker mediatracker = new MediaTracker(component);
			mediatracker.addImage(image, 0);
			mediatracker.waitForAll();
			width = image.getWidth(component);
			height = image.getHeight(component);
			trimWidth = width;
			trimHeight = height;
			offsetX = 0;
			offsetY = 0;
			pixels = new int[width * height];
			PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, width, height, pixels, 0,
			width);
			pixelgrabber.grabPixels();
		} catch (Exception _ex) {
			System.out.println("Error converting jpg");
		}
	}

	/**
	 * Creates a sprite with the given width/height.
	 * 
	 * @param width
	 *            The width.
	 * @param height
	 *            The height.
	 */
	public Sprite(int width, int height) {
		pixels = new int[width * height];
		this.width = trimWidth = width;
		this.height = trimHeight = height;
		offsetX = offsetY = 0;
	}

	/**
	 * Creates a custom image.
	 * 
	 * @param name
	 *            The name of the image.
	 */
	public Sprite(String name) {
		try {
			Image image = Toolkit.getDefaultToolkit().getImage(name);
			ImageIcon sprite = new ImageIcon(Toolkit.getDefaultToolkit().getImage(name));
			width = sprite.getIconWidth();
			height = sprite.getIconHeight();
			trimWidth = width;
			trimHeight = height;
			offsetX = 0;
			offsetY = 0;
			pixels = new int[width * height];
			PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, width, height, pixels, 0,
			width);
			pixelgrabber.grabPixels();
			image = null;
		} catch (Exception _ex) {
			System.out.println(_ex);
		}
	}

	/**
	 * Creates an image from a custom ZIP archive.
	 * 
	 * @param imageName
	 *            The name of the image.
	 * @param requestIndex
	 *            The index of the image.
	 */
	public Sprite(String imageName, int requestIndex) {
		try {
			int index = -1;
			int fileCount;
			long[] fileSizes;
			String[] fileNames;
			byte[] fileBytes = null;
			ZipFile zipfile = new ZipFile(Signlink.findcachedir() + "main_file_cache.idx5");
			DataInputStream datainputstream = new DataInputStream(zipfile
			.getInputStream(new ZipEntry("image_store")));
			fileCount = datainputstream.readInt();
			fileSizes = new long[fileCount];
			fileNames = new String[fileCount];
			for (int i = 0; i < fileCount; i++) {
				fileNames[i] = datainputstream.readUTF();
				fileSizes[i] = datainputstream.readLong();
			}
			for (int i = 0; i < fileNames.length; i++) {
				if (fileNames[i].equalsIgnoreCase(imageName + "_" + requestIndex)) {
					index = i;
					break;
				}
			}
			for (int i = 0; i < fileCount; i++) {
				fileBytes = new byte[(int) (fileSizes[i])];
				datainputstream.readFully(fileBytes, 0, fileBytes.length);
				if (i == index) {
					datainputstream.close();
					break;
				}
			}
			ImageIcon icon = new ImageIcon(fileBytes);
			Image image = Toolkit.getDefaultToolkit().createImage(fileBytes);
			width = icon.getIconWidth();
			height = icon.getIconHeight();
			trimWidth = width;
			trimHeight = height;
			offsetX = 0;
			offsetY = 0;
			pixels = new int[width * height];
			PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, width, height, pixels, 0,
			width);
			pixelgrabber.grabPixels();
			setColorTransparent(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Old method of getting the image dimensions.
	 * 
	 * @param name
	 *            The name and directory of the image.
	 * @param width
	 *            The width of the image.
	 * @param height
	 *            The height of the image.
	 */
	public Sprite(String name, int width, int height) {
		try {
			Image image = Toolkit.getDefaultToolkit().createImage(FileOperations.readFile(name));
			this.width = width;
			this.height = height;
			trimWidth = this.width;
			trimHeight = this.height;
			offsetX = 0;
			offsetY = 0;
			pixels = new int[this.width * this.height];
			PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, this.width, this.height,
			pixels, 0, this.width);
			pixelgrabber.grabPixels();
			image = null;
		} catch (Exception _ex) {
			System.out.println(_ex);
		}
	}

	/**
	 * Adjusts the image colors.
	 * 
	 * @param redOffset
	 *            The red offset.
	 * @param greenOffset
	 *            The green offset.
	 * @param blueOffset
	 *            The blue offset.
	 */
	public void adjustColors(int redOffset, int greenOffset, int blueOffset) {
		for (int i1 = 0; i1 < pixels.length; i1++) {
			int color = pixels[i1];
			if (color != 0) {
				int red = color >> 16 & 0xff;
				red += redOffset;
				if (red < 1) {
					red = 1;
				} else if (red > 255) {
					red = 255;
				}
				int green = color >> 8 & 0xff;
				green += greenOffset;
				if (green < 1) {
					green = 1;
				} else if (green > 255) {
					green = 255;
				}
				int blue = color & 0xff;
				blue += blueOffset;
				if (blue < 1) {
					blue = 1;
				} else if (blue > 255) {
					blue = 255;
				}
				pixels[i1] = (red << 16) + (green << 8) + blue;
			}
		}
	}

	/**
	 * Sets the pixels of an inverse image.
	 * 
	 * @param width
	 *            The width of the original image.
	 * @param height
	 *            The height of the original image.
	 * @param destPixels
	 *            The new image area.
	 * @param srcPixels
	 *            The original image area.
	 * @param destOffset
	 *            The offset * the max width.
	 * @param srcOffset
	 *            The original offset, value should always be 0.
	 * @param destStep
	 *            The max image size - the original image size.
	 * @param srcStep
	 *            The original step, value should always be 0.
	 */
	private void copyPixels(int width, int height, int destPixels[], int srcPixels[],
	int destOffset, int srcOffset, int destStep, int srcStep) {
		int destWidth = -(width >> 2);
		width = -(width & 3);
		for (int i = -height; i < 0; i++) {
			for (int i2 = destWidth; i2 < 0; i2++) {
				destPixels[destOffset++] = srcPixels[srcOffset++];
				destPixels[destOffset++] = srcPixels[srcOffset++];
				destPixels[destOffset++] = srcPixels[srcOffset++];
				destPixels[destOffset++] = srcPixels[srcOffset++];
			}
			for (int i3 = width; i3 < 0; i3++) {
				destPixels[destOffset++] = srcPixels[srcOffset++];
			}
			destOffset += destStep;
			srcOffset += srcStep;
		}
	}

	public void drawAlphaSprite(int x, int y) {
		int k = 256;
		x += offsetX;
		y += offsetY;
		int destOffset = x + y * Renderer2D.width;
		int srcOffset = 0;
		int height = this.height;
		int width = this.width;
		int destStep = Renderer2D.width - width;
		int srcStep = 0;
		if (y < Renderer2D.topY) {
			int trimHeight = Renderer2D.topY - y;
			height -= trimHeight;
			y = Renderer2D.topY;
			srcOffset += trimHeight * width;
			destOffset += trimHeight * Renderer2D.width;
		}
		if (y + height > Renderer2D.bottomY) {
			height -= (y + height) - Renderer2D.bottomY;
		}
		if (x < Renderer2D.topX) {
			int trimLeft = Renderer2D.topX - x;
			width -= trimLeft;
			x = Renderer2D.topX;
			srcOffset += trimLeft;
			destOffset += trimLeft;
			srcStep += trimLeft;
			destStep += trimLeft;
		}
		if (x + width > Renderer2D.bottomX) {
			int trimRight = (x + width) - Renderer2D.bottomX;
			width -= trimRight;
			srcStep += trimRight;
			destStep += trimRight;
		}
		if (!((width <= 0) || (height <= 0))) {
			setAlphaPixels(width, height, Renderer2D.pixels, pixels, k, destOffset, srcOffset,
			destStep, srcStep);
		}
	}

	/**
	 * Draws an IndexedImage? I'm not so sure on this one. It's never used in
	 * the client.
	 * 
	 * @param indexedSprite
	 *            The IndexedImage.
	 * @param x
	 *            The x offset.
	 * @param y
	 *            The y offset.
	 */
	public void drawIndexedImage(IndexedSprite indexedSprite, int x, int y) {
		x += offsetX;
		y += offsetY;
		int destOffset = x + y * Renderer2D.width;
		int srcOffset = 0;
		int height = this.height;
		int width = this.width;
		int destStep = Renderer2D.width - width;
		int srcStep = 0;
		if (y < Renderer2D.topY) {
			int fixedHeight = Renderer2D.topY - y;
			height -= fixedHeight;
			y = Renderer2D.topY;
			srcOffset += fixedHeight * width;
			destOffset += fixedHeight * Renderer2D.width;
		}
		if (y + height > Renderer2D.bottomY) {
			height -= (y + height) - Renderer2D.bottomY;
		}
		if (x < Renderer2D.topX) {
			int fixedWidth = Renderer2D.topX - x;
			width -= fixedWidth;
			x = Renderer2D.topX;
			srcOffset += fixedWidth;
			destOffset += fixedWidth;
			srcStep += fixedWidth;
			destStep += fixedWidth;
		}
		if (x + width > Renderer2D.bottomX) {
			int k2 = (x + width) - Renderer2D.bottomX;
			width -= k2;
			srcStep += k2;
			destStep += k2;
		}
		if (!((width <= 0) || (height <= 0))) {
			setPixels(width, height, 0, Renderer2D.pixels, pixels, indexedSprite.pixels, destStep,
			srcStep, destOffset, srcOffset);
		}
	}

	/**
	 * Copies an image and draws it with the inverse offsets.
	 * 
	 * @param x
	 *            The x offset.
	 * @param y
	 *            The y offset.
	 */
	public void drawInverse(int x, int y) {
		x += offsetX;
		y += offsetY;
		int destOffset = x + y * Renderer2D.width;
		int srcOffset = 0;
		int height = this.height;
		int width = this.width;
		int destStep = Renderer2D.width - width;
		int srcStep = 0;
		if (y < Renderer2D.topY) {
			int fixedHeight = Renderer2D.topY - y;
			height -= fixedHeight;
			y = Renderer2D.topY;
			srcOffset += fixedHeight * width;
			destOffset += fixedHeight * Renderer2D.width;
		}
		if (y + height > Renderer2D.bottomY) {
			height -= (y + height) - Renderer2D.bottomY;
		}
		if (x < Renderer2D.topX) {
			int fixedWidth = Renderer2D.topX - x;
			width -= fixedWidth;
			x = Renderer2D.topX;
			srcOffset += fixedWidth;
			destOffset += fixedWidth;
			srcStep += fixedWidth;
			destStep += fixedWidth;
		}
		if (x + width > Renderer2D.bottomX) {
			int l2 = (x + width) - Renderer2D.bottomX;
			width -= l2;
			srcStep += l2;
			destStep += l2;
		}
		if ((width <= 0) || (height <= 0)) {
		} else {
			copyPixels(width, height, Renderer2D.pixels, pixels, destOffset, srcOffset, destStep,
			srcStep);
		}
	}

	/**
	 * Draws a shaped image, such as the minimap and compass.
	 * 
	 * @param x
	 *            The x position.
	 * @param y
	 *            The y position.
	 * @param width
	 *            The width.
	 * @param height
	 *            The height.
	 * @param destPixels
	 *            The area.
	 * @param srcPixels
	 *            The original area.
	 * @param j
	 * @param k
	 * @param i1
	 * @param i2
	 */
	public void drawShape(int x, int y, int width, int height, int destPixels[], int srcPixels[],
	int j, int k, int i1, int i2) {
		try {
			int j2 = -width / 2;
			int k2 = -height / 2;
			int l2 = (int) (Math.sin(j / 326.11000000000001D) * 65536D);
			int i3 = (int) (Math.cos(j / 326.11000000000001D) * 65536D);
			l2 = l2 * k >> 8;
			i3 = i3 * k >> 8;
			int j3 = (i2 << 16) + (k2 * l2 + j2 * i3);
			int k3 = (i1 << 16) + (k2 * i3 - j2 * l2);
			int offset = x + y * Renderer2D.width;
			for (y = 0; y < height; y++) {
				int i4 = srcPixels[y];
				int j4 = offset + i4;
				int k4 = j3 + i3 * i4;
				int l4 = k3 - l2 * i4;
				for (x = -destPixels[y]; x < 0; x++) {
					Renderer2D.pixels[j4++] = this.pixels[(k4 >> 16) + (l4 >> 16) * this.width];
					k4 += i3;
					l4 -= l2;
				}
				j3 += l2;
				k3 += i3;
				offset += Renderer2D.width;
			}
		} catch (Exception _ex) {
		}
	}

	/**
	 * Draws a regular image.
	 * 
	 * @param x
	 *            The x offset.
	 * @param y
	 *            The y offset.
	 */
	public void drawSprite(int x, int y) {
		x += offsetX;
		y += offsetY;
		int destOffset = x + y * Renderer2D.width;
		int srcOffset = 0;
		int height = this.height;
		int width = this.width;
		int destStep = Renderer2D.width - width;
		int srcStep = 0;
		if (y < Renderer2D.topY) {
			int trimHeight = Renderer2D.topY - y;
			height -= trimHeight;
			y = Renderer2D.topY;
			srcOffset += trimHeight * width;
			destOffset += trimHeight * Renderer2D.width;
		}
		if (y + height > Renderer2D.bottomY) {
			height -= (y + height) - Renderer2D.bottomY;
		}
		if (x < Renderer2D.topX) {
			int trimLeft = Renderer2D.topX - x;
			width -= trimLeft;
			x = Renderer2D.topX;
			srcOffset += trimLeft;
			destOffset += trimLeft;
			srcStep += trimLeft;
			destStep += trimLeft;
		}
		if (x + width > Renderer2D.bottomX) {
			int trimRight = (x + width) - Renderer2D.bottomX;
			width -= trimRight;
			srcStep += trimRight;
			destStep += trimRight;
		}
		if (!((width <= 0) || (height <= 0))) {
			setPixels(width, height, Renderer2D.pixels, pixels, destOffset, srcOffset, destStep,
			srcStep);
		}
	}

	/**
	 * Draws an image with alpha.
	 * 
	 * @param x
	 *            The x offset.
	 * @param y
	 *            The y offset.
	 * @param alpha
	 *            The alpha value. 128 should be 50%.
	 */
	public void drawSprite(int x, int y, int alpha) {
		x += offsetX;
		y += offsetY;
		int destOffset = x + y * Renderer2D.width;
		int srcOffset = 0;
		int height = this.height;
		int width = this.width;
		int destStep = Renderer2D.width - width;
		int srcStep = 0;
		if (y < Renderer2D.topY) {
			int fixedHeight = Renderer2D.topY - y;
			height -= fixedHeight;
			y = Renderer2D.topY;
			srcOffset += fixedHeight * width;
			destOffset += fixedHeight * Renderer2D.width;
		}
		if (y + height > Renderer2D.bottomY) {
			height -= (y + height) - Renderer2D.bottomY;
		}
		if (x < Renderer2D.topX) {
			int fixedWidth = Renderer2D.topX - x;
			width -= fixedWidth;
			x = Renderer2D.topX;
			srcOffset += fixedWidth;
			destOffset += fixedWidth;
			srcStep += fixedWidth;
			destStep += fixedWidth;
		}
		if (x + width > Renderer2D.bottomX) {
			int i3 = (x + width) - Renderer2D.bottomX;
			width -= i3;
			srcStep += i3;
			destStep += i3;
		}
		if (!((width <= 0) || (height <= 0))) {
			setFixedAlphaPixels(width, height, alpha, Renderer2D.pixels, pixels, destOffset,
			srcOffset, destStep, srcStep);
		}
	}

	/**
	 * Draws a sprite with custom height.
	 * 
	 * @param x
	 *            The x position.
	 * @param y
	 *            The y position.
	 * @param height
	 *            The height.
	 */
	public void drawSpriteWithDimensions(int x, int y, int height) {
		x += offsetX;
		y += offsetY;
		int destOffset = x + y * Renderer2D.width;
		int srcOffset = 0;
		int width = this.width;
		int destStep = Renderer2D.width - width;
		int srcStep = 0;
		if (y < Renderer2D.topY) {
			int j2 = Renderer2D.topY - y;
			height -= j2;
			y = Renderer2D.topY;
			srcOffset += j2 * width;
			destOffset += j2 * Renderer2D.width;
		}
		if (y + height > Renderer2D.bottomY) {
			height -= (y + height) - Renderer2D.bottomY;
		}
		if (x < Renderer2D.topX) {
			int k2 = Renderer2D.topX - x;
			width -= k2;
			x = Renderer2D.topX;
			srcOffset += k2;
			destOffset += k2;
			srcStep += k2;
			destStep += k2;
		}
		if (x + width > Renderer2D.bottomX) {
			int l2 = (x + width) - Renderer2D.bottomX;
			width -= l2;
			srcStep += l2;
			destStep += l2;
		}
		if (!((width <= 0) || (height <= 0))) {
			setPixels(width, height, Renderer2D.pixels, pixels, destOffset, srcOffset, destStep,
			srcStep);
		}
	}

	/**
	 * Begins drawing the image.
	 */
	public void init() {
		Renderer2D.init(width, height, pixels);
	}

	public void method353(int i, double d, int l1) {
		// all of the following were parameters
		int j = 15;
		int k = 20;
		int l = 15;
		int j1 = 256;
		int k1 = 20;
		// all of the previous were parameters
		try {
			int i2 = -k / 2;
			int j2 = -k1 / 2;
			int k2 = (int) (Math.sin(d) * 65536D);
			int l2 = (int) (Math.cos(d) * 65536D);
			k2 = k2 * j1 >> 8;
			l2 = l2 * j1 >> 8;
			int i3 = (l << 16) + (j2 * k2 + i2 * l2);
			int j3 = (j << 16) + (j2 * l2 - i2 * k2);
			int k3 = l1 + i * Renderer2D.width;
			for (i = 0; i < k1; i++) {
				int l3 = k3;
				int i4 = i3;
				int j4 = j3;
				for (l1 = -k; l1 < 0; l1++) {
					int k4 = pixels[(i4 >> 16) + (j4 >> 16) * width];
					if (k4 != 0) {
						Renderer2D.pixels[l3++] = k4;
					} else {
						l3++;
					}
					i4 += l2;
					j4 -= k2;
				}
				i3 += k2;
				j3 += l2;
				k3 += Renderer2D.width;
			}
		} catch (Exception _ex) {
		}
	}

	private void setAlphaPixels(int width, int height, int destPixels[], int srcPixels[],
	int srcAlpha, int destOffset, int srcOffset, int destStep, int srcStep) {
		int srcColor;
		int destAlpha;
		for (int loop = -height; loop < 0; loop++) {
			for (int loop2 = -width; loop2 < 0; loop2++) {
				srcAlpha = ((this.pixels[srcOffset] >> 24) & 255);
				destAlpha = 256 - srcAlpha;
				srcColor = srcPixels[srcOffset++];
				if (srcColor != 0) {
					int destColor = destPixels[destOffset];
					destPixels[destOffset++] = ((srcColor & 0xff00ff) * srcAlpha
					+ (destColor & 0xff00ff) * destAlpha & 0xff00ff00)
					+ ((srcColor & 0xff00) * srcAlpha + (destColor & 0xff00) * destAlpha & 0xff0000) >> 8;
				} else {
					destOffset++;
				}
			}
			destOffset += destStep;
			srcOffset += srcStep;
		}
	}

	private void setColorTransparent(int color) {
		for (int pixel = 0; pixel < pixels.length; pixel++) {
			if (((pixels[pixel] >> 24) & 255) == color) {
				pixels[pixel] = 0;
			}
		}
	}

	/**
	 * Sets the pixels of an image with alpha.
	 * 
	 * @param width
	 *            The width.
	 * @param height
	 *            The height.
	 * @param srcAlpha
	 *            The alpha level, 256 is max, lower = less alpha.
	 * @param destPixels
	 *            The new pixels.
	 * @param srcPixels
	 *            The original pixels.
	 * @param destOffset
	 *            The image offset.
	 * @param srcOffset
	 *            The original offset, should always be 0.
	 * @param destStep
	 *            The image step.
	 * @param srcStep
	 *            The original step, should always be 0.
	 */
	private void setFixedAlphaPixels(int width, int height, int srcAlpha, int destPixels[],
	int srcPixels[], int destOffset, int srcOffset, int destStep, int srcStep) {
		int srcColor;
		int destAlpha = 256 - srcAlpha;
		for (int loop = -height; loop < 0; loop++) {
			for (int loop2 = -width; loop2 < 0; loop2++) {
				srcColor = srcPixels[srcOffset++];
				if ((srcColor != 0) && (srcColor != -1)) {
					int destColor = destPixels[destOffset];
					destPixels[destOffset++] = ((srcColor & 0xff00ff) * srcAlpha
					+ (destColor & 0xff00ff) * destAlpha & 0xff00ff00)
					+ ((srcColor & 0xff00) * srcAlpha + (destColor & 0xff00) * destAlpha & 0xff0000) >> 8;
				} else {
					destOffset++;
				}
			}
			destOffset += destStep;
			srcOffset += srcStep;
		}
	}

	private void setPixels(int width, int height, int color, int destPixels[], int srcPixels[],
	byte indexedImagePixels[], int destStep, int srcStep, int destOffset, int srcOffset) {
		int destWidth = -(width >> 2);
		width = -(width & 3);
		for (int i = -height; i < 0; i++) {
			for (int i2 = destWidth; i2 < 0; i2++) {
				color = srcPixels[srcOffset++];
				if ((color != 0) && (indexedImagePixels[destOffset] == 0)) {
					destPixels[destOffset++] = color;
				} else {
					destOffset++;
				}
				color = srcPixels[srcOffset++];
				if ((color != 0) && (indexedImagePixels[destOffset] == 0)) {
					destPixels[destOffset++] = color;
				} else {
					destOffset++;
				}
				color = srcPixels[srcOffset++];
				if ((color != 0) && (indexedImagePixels[destOffset] == 0)) {
					destPixels[destOffset++] = color;
				} else {
					destOffset++;
				}
				color = srcPixels[srcOffset++];
				if ((color != 0) && (indexedImagePixels[destOffset] == 0)) {
					destPixels[destOffset++] = color;
				} else {
					destOffset++;
				}
			}
			for (int i3 = width; i3 < 0; i3++) {
				color = srcPixels[srcOffset++];
				if ((color != 0) && (indexedImagePixels[destOffset] == 0)) {
					destPixels[destOffset++] = color;
				} else {
					destOffset++;
				}
			}
			destOffset += destStep;
			srcOffset += srcStep;
		}
	}

	/**
	 * Sets the pixels of a regular image.
	 * 
	 * @param width
	 *            The width.
	 * @param height
	 *            The height.
	 * @param destPixels
	 *            The new pixels.
	 * @param srcPixels
	 *            The original pixels.
	 * @param destOffset
	 *            The image offset.
	 * @param srcOffset
	 *            The original offset, should always be 0.
	 * @param destStep
	 *            The image step.
	 * @param srcStep
	 *            The original step, should always be 0.
	 */
	private void setPixels(int width, int height, int destPixels[], int srcPixels[],
	int destOffset, int srcOffset, int destStep, int srcStep) {
		int color;
		int destWidth = -(width >> 2);
		width = -(width & 3);
		for (int loop = -height; loop < 0; loop++) {
			for (int loop2 = destWidth; loop2 < 0; loop2++) {
				color = srcPixels[srcOffset++];
				if ((color != 0) && (color != -1)) {
					destPixels[destOffset++] = color;
				} else {
					destOffset++;
				}
				color = srcPixels[srcOffset++];
				if ((color != 0) && (color != -1)) {
					destPixels[destOffset++] = color;
				} else {
					destOffset++;
				}
				color = srcPixels[srcOffset++];
				if ((color != 0) && (color != -1)) {
					destPixels[destOffset++] = color;
				} else {
					destOffset++;
				}
				color = srcPixels[srcOffset++];
				if ((color != 0) && (color != -1)) {
					destPixels[destOffset++] = color;
				} else {
					destOffset++;
				}
			}
			for (int loop3 = width; loop3 < 0; loop3++) {
				color = srcPixels[srcOffset++];
				if ((color != 0) && (color != -1)) {
					destPixels[destOffset++] = color;
				} else {
					destOffset++;
				}
			}
			destOffset += destStep;
			srcOffset += srcStep;
		}
	}

	/**
	 * Trims the image.
	 */
	public void trim() {
		int newSize[] = new int[trimWidth * trimHeight];
		for (int j = 0; j < height; j++) {
			System.arraycopy(pixels, j * width, newSize, j + offsetY * trimWidth + offsetX, width);
		}
		pixels = newSize;
		width = trimWidth;
		height = trimHeight;
		offsetX = 0;
		offsetY = 0;
	}
}
